<?php
	namespace Home\Controller;
	use  Think\Controller;
	class ScartController extends Controller{
		
		//跳转到购物车页
		public function index(){	

			//实例化model类
			$cart = M('cart');

			//开启事物
			$cart->startTrans();

			//将图书添加到购物车表
			 $carta['uid'] = $uid = session('user_id');
			
			$carta['bid'] = I('get.id'); 
			
			$res = $cart->add($carta);
			if($res){
			
			//查询出购物车信息
			$data = $cart->table('__CART__ c,__BOOK__ b')->where("c.uid=$uid and c.bid = b.id")->select();
				if($data){
					//执行添加
					$cart->commit();

					//赋值变量
					$this->assign(data,$data);
					//输出模版
					$this->display();
				}else{
					//回滚
					$cart->rollback();
				}
			

			}else{
				//回滚
				$cart->rollback();
				$this->error('加入购物车失败');
			}

			
			
		}
	}